package cn.data.collection.core;

import java.util.HashMap;
import java.util.Map;

import org.jsoup.nodes.Document;

import cn.data.collection.constant.BusConstant;
import cn.data.collection.constant.CollectionParaConstant;
import cn.data.collection.constant.ThreadCommonData;
import cn.data.collection.exception.CollectionException;
import cn.data.collection.util.HttpClientUtils;
import cn.data.collection.util.PropertiesUtils;

public class CollenctionCore {

	public CollenctionCore() {
		super();
	}

	private HttpClientUtils httpClientUtils = new HttpClientUtils();

	public void initSetup() throws CollectionException {
		try {
			httpClientUtils.initHttpClient();
			Document doc = httpClientUtils.instr2Jsoup(httpClientUtils.loadUrl(CollectionParaConstant.POST_URL));
			CollectionParaConstant.__EVENTTARGET = doc.select("#__EVENTTARGET").val();
			CollectionParaConstant.__EVENTARGUMENT = doc.select("#__EVENTARGUMENT").val();
			CollectionParaConstant.__VIEWSTATE = doc.select("#__VIEWSTATE").val();
			CollectionParaConstant.__VIEWSTATEGENERATOR = doc.select("#__VIEWSTATEGENERATOR").val();
			CollectionParaConstant.__EVENTVALIDATION = doc.select("#__EVENTVALIDATION").val();
			CollectionParaConstant.txtSPROCESS = doc.select("#txtSPROCESS").val();
		} catch (Exception e) {
			throw new CollectionException(e);
		} finally {
			httpClientUtils.releaseConnection();
		}
	}

	public String exec(String admissibility) throws CollectionException {
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("__EVENTTARGET", CollectionParaConstant.__EVENTTARGET_VAL);
			paramMap.put("__EVENTARGUMENT", CollectionParaConstant.__EVENTARGUMENT);
			paramMap.put("__VIEWSTATE", CollectionParaConstant.__VIEWSTATE);
			paramMap.put("__VIEWSTATEGENERATOR", CollectionParaConstant.__VIEWSTATEGENERATOR);
			paramMap.put("__EVENTVALIDATION", CollectionParaConstant.__EVENTVALIDATION);
			paramMap.put("txtSPROCESS", admissibility);
			String strConent = httpClientUtils.postForm(CollectionParaConstant.POST_URL, paramMap);
			Document doc = httpClientUtils.instr2Jsoup(strConent);
			String lbSNAME = doc.select("#lbSNAME").html();
			String lbSTYPE = doc.select("#lbSTYPE").html();
			String lbSSTATUSTYPE = doc.select("#lbSSTATUSTYPE").html();
			if (lbSNAME != null && !"".equals(lbSNAME)&&CollectionParaConstant.LBSTYPE_KEYWORDS.equals(lbSTYPE)) {
				if (CollectionParaConstant.instatustype.indexOf(lbSSTATUSTYPE) == -1) {
					return BusConstant.B_80002.getCode();
				}
				String[] companyKeyWordArray = CollectionParaConstant.companyKeyWords.split("\\|");
				for (int i = 0; i < companyKeyWordArray.length; i++) {
					if (lbSNAME.indexOf(companyKeyWordArray[i]) != -1) {
						return lbSNAME;
					}
				}
				return BusConstant.B_80002.getCode();
			}
			return null;
		} catch (Exception e) {
			throw new CollectionException(e);
		}
	}

	public void releaseConnection() {
		httpClientUtils.releaseConnection();
	}

	public void initCollectionPools() {
		httpClientUtils.initHttpClientPools();
	}

	public static void main(String[] args) throws Exception {
		System.out.println(ThreadCommonData.HAVED_COLLECTION_COMPANY.get("05201510280017"));
		PropertiesUtils.getInstance();
		CollenctionCore objCollenctionCore = new CollenctionCore();
		objCollenctionCore.initSetup();

		System.out.println("__EVENTTARGET:" + CollectionParaConstant.__EVENTTARGET);
		System.out.println("__EVENTARGUMENT:" + CollectionParaConstant.__EVENTARGUMENT);
		System.out.println("__VIEWSTATE:" + CollectionParaConstant.__VIEWSTATE);
		System.out.println("__VIEWSTATEGENERATOR:" + CollectionParaConstant.__VIEWSTATEGENERATOR);
		System.out.println("__EVENTVALIDATION:" + CollectionParaConstant.__EVENTVALIDATION);
		System.out.println("txtSPROCESS:" + CollectionParaConstant.txtSPROCESS);
		try {
			CollenctionCore objExecCollenctionCore = new CollenctionCore();
			objExecCollenctionCore.httpClientUtils.initHttpClient();
			String company = objExecCollenctionCore.exec("05201510280017");
			System.out.println(company);
			System.out.println(company != null && company != "");
		} catch (Exception e) {

		}

		System.out.println("".indexOf("公司||厂"));
	}

}
